<?php

namespace App\Http\Controllers\Admin;

use App\Admin\Album;
use App\Admin\Banner;
use App\Admin\Single;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class AlbumController extends Controller {
	//get Admin/album 全部相册列表
	public function index(Request $request) {
		$album = Album::select()->get();
		return view('admin.carousel.carousel', compact('album'));
	}
//    相册排序
	public function changeOrder(Request $request) {
		$input = $request->all();
		$album = Album::find($input['id']);
		$album->order = $input['order'];
		$res = $album->save();
		if ($res) {
			$data = ['status' => 0,
				'msg' => '图片排序更新成功',

			];
		} else {
			$data = [
				'status' => 1,
				'msg' => '图片排序更新失败，请重试',
			];
		}
		return $data;
	}
	public function show(Request $request) {

	}
	//get Admin/carousel/create 添加用户信息
	public function create() {
		return view('admin/carousel/caradd');
	}
	//图片上传
	public function upload() {
		$file = Input::file('Filedata');
		if ($file->isValid()) {
			$entension = $file->getClientOriginalExtension(); //上传文件的后缀
			$newName = date('YmdHis') . mt_rand(100, 900) . '.' . $entension;
			$path = $file->move(base_path() . '/public/imges', $newName);
			$filepath = '/imges/' . $newName;
			return $filepath;
		}
	}
	//图片添加
	public function changeadd(Request $request) {
		$input = Input::except('_token');
		$input['ctime'] = time();
//        return $input;
		//        $rules = [
		//            'username'=>'required',
		//            'password'=>'required'
		//        ];
		//        $message = [
		//            'username.required'=>'角色名不能为空!',
		//            'password.required'=>'密码不能为空'
		//        ];
		//        $this->validate($request,$rules,$message);
		$res = Album::create($input);
		if ($res) {
			$data = ['status' => 0,
				'msg' => '图片添加成功',
			];
		} else {
			$data = [
				'status' => 1,
				'msg' => '图片添加失败，请重试',
			];
		}
		return $data;

	}
	//post Admin/user 添加用户信息提交
	public function store(Request $request) {

	}
	//get Admin/user/{user}/edit   编辑用户信息
	public function edit($id) {
		$album = Album::find($id);
		return view('admin/carousel/caredit', compact('album'));

	}
	//相册修改
	public function changedit(Request $request) {
		$id = $request->id;
		$album_name = $request->name;
		$input = Input::except('_token');
		$input['ctime'] = time();
//        return $input;
		//        $rules = [
		//            'name'=>'required',
		//            'path'=>'required'
		//        ];
		//        $message = [
		//            'name.required'=>'角色名不能为空!',
		//            'path.required'=>'密码不能为空'
		//        ];
		//        $this->validate($request,$rules,$message);
		$alb = Album::where('id', $id)->select()->get();
		$name = $alb[0]->name;
		$res = Album::where('id', $id)->update($input);
		if ($res) {
			DB::table('banner')->where('album_name', $name)->update(['album_name' => $album_name]);
			$data = ['status' => 0,
				'msg' => '相册更新成功',
			];
		} else {
			$data = [
				'status' => 1,
				'msg' => '相册更新失败，请重试',
			];
		}
		return $data;

	}
	public function chan(Request $request) {
		$input = Input::except('_token');
		$input['ctime'] = time();
		foreach ($request->path as $p) {
			$filename = date('YmdHis') . mt_rand(100, 900) . '.' . $p->getClientOriginalExtension();
			if (!$p->move(base_path() . '/public/imges', $filename)) {
				$data = [
					'status' => 1,
					'msg' => '图片未选中',
				];
				return $data;
			} else {
				$res = DB::table('banner')->insert([
					'album_name' => $request->album_name,
					'name' => $request->name,
					'describe' => $request->describe,
					'ctime' => time(),
					'order' => $request->order,
					'path' => '/imges/' . $filename,
				]);

			}
		}
		if ($res) {
			$data = ['status' => 0,
				'msg' => '图片添加成功，请查看',
			];
		} else {
			$data = [
				'status' => 1,
				'msg' => '图片添加失败，请重试',
			];
		}
		return $data;

	}
	//put Admin/navs/{navs} 更新用户信息
	public function update($id) {

	}

	//delete Admin/album/{album}  删除用户信息
	public function destroy($id) {
		$alb = Album::where('id', $id)->select()->get();
		$albs = $alb[0]->name;
		$path = $alb[0]->path;
		$str = str_replace('/', '\\', $path);
		$album = Banner::where('album_name', $albs)->get()->toArray();
		if ($album) {
			$data = [
				'status' => 1,
				'msg' => '含有相片相册不能删除!',
			];
			return $data;
		} else {
			$res = Album::where('id', $id)->delete();
			if ($res) {
				@unlink(public_path() . $path);
				$data = [
					'status' => 0,
					'msg' => '相册删除成功!',
				];
			} else {
				$data = [
					'status' => 1,
					'msg' => '相册删除失败!',
				];
			}
			return $data;
		}
	}

	public function child(Request $request) {
//        $input = Input::except('_token');
		$album = $request->date;
		// return $album;
		$album = Banner::where('album_name', $album)->select()->get();
		$td = '';
		foreach ($album as $a) {

			$td .= '<div style="float:left"><img src="' . $a->path . '" style="size:100px;float:left"></div>';

		}
		$str = $td;
//        if($res){
		//            $data = [
		//                'status'=>0,
		//                'msg'=>'自定义导航删除成功!',
		//            ];
		//        }else{
		//            $data = [
		//                'status'=>1,
		//                'msg'=>'自定义导航删除失败!',
		//            ];
		//        }
		return $str;
	}

	public function repeact(Request $request) {
		$input = Input::except('_token');
		$ass = $input['ass'];
		$img = $input['data'];
		$res = Album::where('name', $ass)->update(['path' => $img]);
		if ($res) {
			$data = [
				'status' => 0,
				'msg' => '相片封面更新成功!',
			];
		} else {
			$data = [
				'status' => 1,
				'msg' => '相册封面失败!',
			];
		}
		return $data;
	}
	//get Admin/user 全部用户列表
	public function mixlist() {
		$album = Album::select()->get();
		$single = Single::select()->get();
		return view('mixlist', compact('album', 'single'));
	}
}
